package com.yuju.controller.roomStatus;

import com.yuju.enums.ReturnStatusEnum;
import com.yuju.enums.StatusEnum;
import com.yuju.hotel.model.RoomModel;
import com.yuju.hotel.service.IRoomService;
import com.yuju.order.enums.RoomStatusReturnCodeEnum;
import com.yuju.roomStatus.model.CheckinRoomModel;
import com.yuju.roomStatus.model.RoomStatusPrintModel;
import com.yuju.roomStatus.service.IRoomStatusService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.Result;
import com.yuju.controller.base.BaseController;
import com.yuju.roomStatus.model.RoomStatusModel;
import com.yuju.vo.roomstatus.RoomStatusVO;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 
 * 类名：RoomStatusController  <br />
 *
 * 功能：
 *
 * @author : jiangp <br />
 * @Date : 2017年12月10日 下午2:53:47  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("roomStatus")
public class RoomStatusController extends BaseController<RoomStatusVO, RoomStatusModel> {

	@Autowired
	private IRoomStatusService roomStatusService;

	@Autowired
	private IRoomService roomService;

	/**
	 * 
	 * 功能：获取可预订房间<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 上午10:25:02 <br/>
	 */
	@RequestMapping("getReserveEnableRoom")
	@ResponseBody
	public Result getReserveEnableRoom(@RequestBody JSONObject obj){
		RoomStatusModel roomStatusModel = obj.toJavaObject(RoomStatusModel.class);
		Result result = new Result();
		List<RoomStatusModel> list = roomStatusService.getReserveEnableRoom(roomStatusModel);

		if (list != null) {
			result.setRows(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_RESERVEROOM_GET_SUCCESS.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_RESERVEROOM_GET_SUCCESS.getDesc());
		}
		else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_RESERVEROOM_GET_FAILURE.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_RESERVEROOM_GET_FAILURE.getDesc());
		}
		return result;
		
	}

	/**
	 * 
	 * 功能：获得可入住房间<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 上午11:21:05 <br/>
	 */
	@RequestMapping("getCheckinEnableRoom")
	@ResponseBody
	public Result getCheckinEnableRoom(@RequestBody JSONObject obj){
		CheckinRoomModel checkinRoomModel = obj.toJavaObject(CheckinRoomModel.class);
		checkinRoomModel.setCheckinTime(obj.getDate("beginDay"));
		checkinRoomModel.setCheckoutTime(obj.getDate("endDay"));
		
		Result result = new Result();
		List<CheckinRoomModel> list = roomStatusService.getCheckinEnableRoom(checkinRoomModel);

		if (list != null) {
			result.setRows(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_CHECKINROOM_GET_SUCCESS.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_CHECKINROOM_GET_SUCCESS.getDesc());
		}
		else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_CHECKINROOM_GET_FAILURE.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_CHECKINROOM_GET_FAILURE.getDesc());
		}
		return result;

	}

	/**
	 * 
	 * 功能：获取当前房态<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 上午10:24:56 <br/>
	 */
	@RequestMapping("getCurrentRoomStatus")
	@ResponseBody
	public Result getCurrentRoomStatus(@RequestBody JSONObject obj) {
		RoomStatusModel roomStatusModel = obj.toJavaObject(RoomStatusModel.class);

		roomStatusModel.setBeginDay(new Date());
		roomStatusModel.setEndDay(new Date());
		Result result = new Result();
		List<RoomStatusModel> list = roomStatusService.getRoomStatusCurrent(roomStatusModel);


		//统计在住房间数，空房数和维修房间数
		Integer liveCount = 0;
		Integer emptyCount = 0;
		Integer recordCount = 0;
		for (RoomStatusModel item:list ) {
			if (item.getIsLive().equals(StatusEnum.TRUE.getValue())) {
				liveCount++;
			}
			else if (item.getIsRecord().equals(StatusEnum.TRUE.getValue())) {
				recordCount++;
			}
			else {
				emptyCount++;
			}
		}
		Map<String,Object> sum = new HashedMap();
		sum.put("liveCount",liveCount);
		sum.put("emptyCount", emptyCount);
		sum.put("recordCount", recordCount);
		if (list != null) {
			result.setRows(list);
			result.setData(sum);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_CURRENT_GET_SUCCESS.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_CURRENT_GET_SUCCESS.getDesc());
		}
		else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_CURRENT_GET_FAILURE.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_CURRENT_GET_FAILURE.getDesc());
		}

		return result;
	}

	/**
	 * 
	 * 功能：获得远期房态<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 上午11:25:50 <br/>
	 */
	@RequestMapping("getForwardRoomStatus")
	@ResponseBody
	public Result getForwardRoomStatus(@RequestBody JSONObject obj) {
		RoomStatusModel roomStatusModel = obj.toJavaObject(RoomStatusModel.class);
		Integer days = obj.getInteger("days");
		Result result = new Result();
		List<Map<String,Object>> list
				= roomStatusService.getRoomStatusForward(roomStatusModel.getHotelId(),
				roomStatusModel.getRoomTypeId(), roomStatusModel.getRoomId(), days);
		if (list != null) {
			result.setRows(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_FORWARD_GET_SUCCESS.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_FORWARD_GET_SUCCESS.getDesc());
		}
		else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_FORWARD_GET_FAILURE.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_FORWARD_GET_FAILURE.getDesc());
		}

		return result;
	}

	/**
	 *
	 * 功能：批量置脏<br/>
	 *
	 * @author jiangp
	 * @Date 2018年1月26日 上午11:25:50 <br/>
	 */
	@RequestMapping("setDirtyByBatch")
	@ResponseBody
	public Result setDirtyByBatch(@RequestBody JSONObject obj) {
		Result result = new Result();
		String idBatch = obj.getString("idBatch");
		Byte isDirty = obj.getByte("isDirty");
		if (idBatch == null || isDirty == null) {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_BATCH_DIRTY_SET_FAILURE.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_BATCH_DIRTY_SET_FAILURE.getDesc());

			return result;
		}
		List<String> ids = Arrays.asList(idBatch.split(","));
		RoomModel roomModel = new RoomModel();
		roomModel.setIds(ids);;
		roomModel.setIsDirty(isDirty);
		int a = roomService.updateByIds(roomModel);

		if (a > 0) {
			result.setRows(null);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_BATCH_DIRTY_SET_SUCCESS.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_BATCH_DIRTY_SET_SUCCESS.getDesc());
		}
		else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_BATCH_DIRTY_SET_FAILURE.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_BATCH_DIRTY_SET_FAILURE.getDesc());
		}

		return result;
	}

	/**
	 *
	 * 功能：获取当前房态<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 上午10:24:56 <br/>
	 */
	@RequestMapping("getRoomStatusPrint")
	@ResponseBody
	public Result getRoomStatusPrint(@RequestBody JSONObject obj) {
		RoomStatusModel roomStatusModel = obj.toJavaObject(RoomStatusModel.class);

		roomStatusModel.setBeginDay(new Date());
		roomStatusModel.setEndDay(new Date());
		Result result = new Result();
		List<RoomStatusPrintModel> list = roomStatusService.getRoomStatusPrint(roomStatusModel);
		Integer totalCount = 0;
		for (int i = 0; i < list.size(); i++) {
			totalCount = totalCount + list.get(i).getRoomCount();
		}
		Map<String,Object> total = new HashedMap();
		total.put("totalCount",totalCount);
		if (list != null) {
			result.setRows(list);
			result.setData(total);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_CURRENT_GET_SUCCESS.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_CURRENT_GET_SUCCESS.getDesc());
		}
		else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_CURRENT_GET_FAILURE.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_CURRENT_GET_FAILURE.getDesc());
		}

		return result;
	}

	/**
	 *
	 * 功能：获取房间管理数据<br/>
	 *
	 * @author jiangp
	 * @Date 2017年12月13日 上午10:24:56 <br/>
	 */
	@RequestMapping("getRoomStatusManage")
	@ResponseBody
	public Result getRoomStatusManage(@RequestBody JSONObject obj) {
		RoomStatusModel roomStatusModel = obj.toJavaObject(RoomStatusModel.class);

		roomStatusModel.setBeginDay(new Date());
		roomStatusModel.setEndDay(new Date());
		Result result = new Result();
		List<RoomStatusModel> list = roomStatusService.getRoomStatusManage(roomStatusModel);
		if (list != null) {
			result.setRows(list);
			result.setData(null);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_ROOM_MANAGE_GET_SUCCESS.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_ROOM_MANAGE_GET_SUCCESS.getDesc());
		}
		else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomStatusReturnCodeEnum.ROOMSTATUS_ROOM_MANAGE_GET_FAILURE.getCode());
			result.setReturnMsg(RoomStatusReturnCodeEnum.ROOMSTATUS_ROOM_MANAGE_GET_FAILURE.getDesc());
		}

		return result;
	}


}
